package cn.henu.soft;
//URL是可以在jar包中加载文件的，流也可以，以file开头的URI和字符串都不可以
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;

public class TestWave {
    public static void main(String[] args) throws UnsupportedEncodingException, URISyntaxException {
        Class cls = TestWave.class;
        String jpgPath= cls.getResource("/image/peakcock.jpg").getFile();
        URL jpgPathURL =cls.getResource("/image/peakcock.jpg");
        String wavePath=cls.getResource("/sound/dianming.wav").getFile();
        System.out.println(wavePath);
        wavePath=URLDecoder.decode(wavePath,"utf-8");
        System.out.println(wavePath);

        URL wavePathURL=cls.getResource("/sound/dianming.wav");
        URI wavePathURI=cls.getResource("/sound/dianming.wav").toURI();
        System.out.println("URI"+wavePathURI);

        jpgPath=URLDecoder.decode(jpgPath,"utf-8");
        System.out.println(jpgPath);
        System.out.println(jpgPathURL);
        try {
            // 加载音频文件
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(wavePathURI));//new File(wavePath)//"H:\\code\\dianming.wav"

            // 创建音频播放器
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);

            // 播放音频
            clip.start();

            // 播放一段时间后停止音频播放
            Thread.sleep(5000);
            clip.stop();

        } catch (Exception e) {
            e.printStackTrace();
        }
//        MyFrame jFrame = new MyFrame("测试图片",jpgPath);
//        jFrame.setBounds(new Rectangle(100,100,300,400));
//        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        //JPanel jPanel =jFrame.getContentPane();
//        jFrame.show();
        JFrame jf=new JFrame("背景图片测试");
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；

        ImageIcon bg=new ImageIcon(jpgPathURL);
        JLabel label=new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel pan=(JPanel)jf.getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new FlowLayout());
        //3.之后把组件和面板添加到窗口面板就可以；
        JButton btn=new JButton("测试按钮");
        pan.add(btn);
        jf.setSize(bg.getIconWidth(),bg.getIconHeight());
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);


    }

}
/*  bat 文件
@echo off
if exist .\cn\henu\soft2 (
echo "exist"
java cn.henu.soft.TestWave
)else (
jar -xf MultiMedia.jar
java cn.henu.soft.TestWave)
 */

//class MyFrame extends  JFrame{
//    String backGroundPic;
//    @Override
//    public void paint(Graphics g) {
//
//        ImageIcon icon=new ImageIcon("C:\\Users\\Administrator\\Pictures\\qq.jpg");
//        g.drawImage(iocn, 0, 0, width, height, null);
//        super.paint(g);
//    }
//    public MyFrame(String title,String backGroundPic){
//        super(title);
//       this.backGroundPic= backGroundPic;
//    }
//}